package com.glela.platform.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.glela.common.constant.CommonConstants;
import com.glela.common.util.MurmurHash3;
import com.glela.common.util.ValidateUtil;
import com.glela.platform.mapper.CGuestMapper;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.Guest;
import com.glela.platform.model.request.RequestGuest;
import com.glela.platform.service.SecurityService;

/**
 * @author liujie
 *
 */
@Service("securityService")
public class SecurityServiceImpl implements SecurityService {
	public static Logger logger = LoggerFactory
			.getLogger(SecurityServiceImpl.class);

	@Autowired
	UserMapper userMapper;

	@Autowired
	CGuestMapper guestMapper;

	private boolean isSimulator(String guestId,int type) {
		boolean isSimulator = true;
		switch (type) {
		case CommonConstants.TYPE_ANDROID:
			if (ValidateUtil.checkIMEI(guestId)) {
				isSimulator = false;
			}
			break;
		case CommonConstants.TYPE_IOS:
			if (ValidateUtil.checkUUID(guestId)) {
				isSimulator = false;
			}
			break;
		default:
			break;
		}
		return isSimulator;
	}

	@Override
	public String getDeviceLink(RequestGuest param) {
		String guestId = param.getGuestId();
		String result="";
		try {
			result = MurmurHash3.tranDcode(guestId);
			Guest guest = guestMapper.selectByCode(result);

			if (guest != null) {

			} else {
				int type=param.getType();
				boolean isSimulator=isSimulator(guestId,type);
				guest = new Guest();
				guest.setCode(result);
				guest.setDebugger(isSimulator);
				guest.setJailbreak(isSimulator);
				guest.setSimulator(isSimulator);
				guest.setChannalId(param.getChannelId());
				guest.setDeviceType(type);

				guestMapper.insert(guest);
			}
		} catch (Exception e) {
			result = "";
			logger.error("获取匿名设备发生异常：" + e);
		}
		return result;
	}

	/**
	 * 设备验证
	 * 
	 * @param deviceCode
	 * @return
	 */
	@Override
	public boolean mobileAuthenticate(String deviceCode) {
		boolean result = false;

		Guest guest = guestMapper.selectByCode(deviceCode);
		if (guest == null)
			return result;
		result = !guest.isSimulator();
		logger.info("设备deviceCode：" + deviceCode + ",认证结果：" + result);
		return result;
	}

	@Override
	public int flag() {
		return userMapper.selectSystemFlag("security");
	}

	@Override
	public boolean isGuest(String guestId) {
		return guestId
				.matches("[0-9a-fA-F]{4}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{8}");
	}

	@Override
	public boolean deviceAuthenticate(String guestCode) {
		boolean result = false;
		if (guestCode == null||guestCode.isEmpty())
			return false;
		/*
		 * 是否使用安全验证
		 */
		boolean isSecurityAuth = isGuest(guestCode);

		if (isSecurityAuth) {
			// 后续加强验证
			result = mobileAuthenticate(guestCode);
		} else {
			result = mobileAuthenticate(guestCode);
		}

		return result;
	}

	public static void main(String[] args) {
		System.out.println("---"
				+ new SecurityServiceImpl().isGuest("000000000000000"));
	}

}
